package reservation.flight;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import reservation.flight.*;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MiReservationActivity extends Activity {
	Button b_regresar;
	ReservationTitle[] datos;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
		Function fun=new Function();
        Bundle bu= getIntent().getExtras();
        String usuario=bu.getString("ID");
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);     
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("opcion","reservaciones"));
        nameValuePairs.add(new BasicNameValuePair("usuario",usuario));
        String result= fun.conexion(nameValuePairs);
	     datos=fun.devolverMisReservacion(result);

		 Log.e("Log","array datos: "+ datos.length);
		 
	    setContentView(R.layout.my_reservation_list);	 
	    
	    
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);
	    adaptador_menu_principal adaptador =
            new adaptador_menu_principal(this, usuario,datos);
     
        ListView listmenu = (ListView)findViewById(R.id.lista);
        listmenu.setAdapter(adaptador);
        
        Button b_salir=(Button)findViewById(R.id.BotonRegresar);
        b_salir.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        
	 	    
	}	
}
class adaptador_menu_principal extends ArrayAdapter{
	Activity context;
	ReservationTitle[] datos;
	adaptador_menu_principal(Activity context, String usuario, ReservationTitle[] datos) {
	     super(context, R.layout.mi_reservacion, datos);
		Log.e("Log","Entro a adaptador Usuario: "+ usuario);		
	    		
			this.context =context;
			this.datos=datos;
			 Log.e("Log","Strng en el adaptador: "+ datos.length);
		// TODO Auto-generated constructor stub
	}
	public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View item = inflater.inflate(R.layout.mi_reservacion, null);
 
        TextView l_origen = (TextView)item.findViewById(R.id.MR_Depart_Label_TW);
        l_origen.setText(datos[position].getOrigen());
        TextView l_destino = (TextView)item.findViewById(R.id.MR_Arrive_Label_TW);
        l_destino.setText(datos[position].getDestino());
        TextView l_salida = (TextView)item.findViewById(R.id.MR_Depart_Date_label_TW);
        l_salida.setText(datos[position].getSalida());
        TextView l_regreso = (TextView)item.findViewById(R.id.MR_Retur_Date_label_TW);
        l_regreso.setText(datos[position].getRegreso());
        
        return(item);
    }
}
